package com.xu.sms.controller;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.xu.sms.R;
import com.xu.sms.adapter.FBOInfoAdapter;
import com.xu.sms.entity.Info;
import com.xu.sms.net.HTTPNet;
import com.xu.sms.utils.LogUtil;

public class FBOInfoActivity extends Activity{
	
	private Context context;
	private Intent intent;
	private ListView fboinfo_lv_list;
	private FBOInfoAdapter fboInfoAdapter;
	public static final int TYPEID_ERROR_STATUS_CODE = 0;
	public static final int INFO_SEND_MSG_STATUS_CODE = 1;
	public final static String TAG = FBOInfoActivity.class.getSimpleName();
	private String content;
	private Integer typeId ;
	private String keyword ;
	private String  requestUrl = "http://"+HTTPNet.servet_IP_Address+"/sms/admin/info/findInfoByTypeId?typeId=";
	private String requestUrl2 = "http://"+HTTPNet.servet_IP_Address+"/sms/admin//info/findInfoBykeyWord?keyword=";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fboinfo);
		//设置列表
		fboinfo_lv_list = (ListView) findViewById(R.id.fboinfo_lv_list);
		context = this;
		intent = getIntent();
		typeId = intent.getIntExtra("typeId", TYPEID_ERROR_STATUS_CODE);
		keyword = intent.getStringExtra("keyword");
		if(typeId == TYPEID_ERROR_STATUS_CODE){
			HTTPNet.requestServerForGet(handler, requestUrl2+keyword);
		}else{
			HTTPNet.requestServerForGet(handler, requestUrl+typeId);
		}
		
	}
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			boolean fibys = (msg.what == HTTPNet.FIND_INFO_BY_TYPE_STATE);
			boolean fibks = (msg.what == HTTPNet.FIND_INFO_BY_KEYWORD_STATE);
			if(fibys ||fibks ){
				List<Info> infoList = (List<Info>) msg.obj;
				if(infoList !=null && infoList.size() >0){
					//设置适配器
					if((fboInfoAdapter == null) && (typeId !=TYPEID_ERROR_STATUS_CODE)){
						fboInfoAdapter = new FBOInfoAdapter(context, infoList,handler,null,typeId);
					}else{
						fboInfoAdapter = new FBOInfoAdapter(context, infoList,handler,keyword,null);
					}
					fboinfo_lv_list.setAdapter(fboInfoAdapter);
				}else{
					Toast.makeText(context, "对不起主人，臣妾做不到", Toast.LENGTH_SHORT).show();
				}
			}else if(msg.what ==INFO_SEND_MSG_STATUS_CODE){
				sendMsg(msg);
			}
		}
		
		private void sendMsg(android.os.Message msg) {
			String content = (String) msg.obj;
			LogUtil.i(TAG, 73, content);
			FBOInfoActivity.this.content = content;
			Intent intent = new Intent();
			intent.setAction("android.intent.action.SEND");//执行的动作
			intent.addCategory("android.intent.category.DEFAULT");//动作分类
			intent.setType("text/plain"); //数据类型
			//【5】将数据传递吊 短信的方页面
			intent.putExtra("sms_body", content);
			startActivity(intent);
		}
	};
	 
	
	//实现标题的置换为类型的名称
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		 getMenuInflater().inflate(R.menu.menu_type, menu);
		 MenuItem item = menu.findItem(R.id.typeName);
		 //从intent获取类型数据
		 String typeName = intent.getStringExtra("typeName");
		 String _content = "";
		 if(typeName !=null){
			 _content = typeName;
		 }else{
			 _content = "\""+keyword +"\""+"搜索结果";
		 }
		 //请求服务器获取信息条目的内容
		 item.setTitle(_content);
		return super.onCreateOptionsMenu(menu);
	}
}






















